import { Component, OnInit } from '@angular/core';
import { CommService } from '../../../ptpmc/comm/comm.service';
import { ReportUrls } from '../../../ptpmc/comm/report.urls.conf';
import { UserModel } from '../../../ptpmc/model/sys/user-model';
declare var $: any;
declare var Ps: any;
class LeftMenu {
    menuId: string;
    parentId: string;
    routerLinkActive: string;
    routerLink: string;
    class: string;
    name: string;
    subMenus: Array<LeftMenu>;
}

@Component({
    selector: 'app-sidebar',
    templateUrl: './sidebar_pt.component.html',
    styleUrls: ['./sidebar_pt.component.scss'],
})
export class SidebarPTComponent implements OnInit {

    leftMenuTree: LeftMenu;
    flatMenuItemArray: Array<LeftMenu>;

    constructor(private commService: CommService) { }

    ngOnInit() {
        this.genLeftMenu();
    }

    /**
     * 根据登陆的用户动态生成左边菜单栏
     */
    genLeftMenu() {

        this.commService.doGet(ReportUrls.menuTree + '/' + UserModel.userId)
            .subscribe(model => {
                if (model.resultCode === 200) {
                   
                    const temp = new Array();
                    this.flatMenuItemArray = new Array();
                    model.data.forEach(menu => {
                        const id = menu.id;
                        const parentId = menu.parentId;
                        const href = menu.href;
                        const name = menu.name;
                        const sort = menu.sort;
                        const isShow = menu.isShow;
                       
                        if (isShow === '1') {
                            this.flatMenuItemArray.push(this.createTreePoint(menu));
                            // 创建根结点
                            if (id === parentId) {
                                this.leftMenuTree = this.createTreePoint(menu);
                            }
                        }
                    });

                    /**
                     * 动态创建菜单栏组件数据，目前仅支持两级
                     */
                    let groupMap = new Map<string, Array<LeftMenu>>();
                    this.flatMenuItemArray.forEach(item => {
                        const parentId = item.parentId;
                        const menuId = item.menuId;
                        if (groupMap.get(parentId) === undefined) {
                            groupMap.set(parentId, new Array());
                        }
                        if (parentId !== menuId) {
                            groupMap.get(parentId).push(item);
                        }
                    });
                    groupMap = this.flatMap(groupMap, 'gncd');
                    this.leftMenuTree.subMenus = groupMap.get('gncd');
                }
            });
    }

    createTreePoint(item: any): LeftMenu {
        const leftMenuTree = new LeftMenu();
        leftMenuTree.menuId = item.id;
        leftMenuTree.parentId = item.parentId;
        leftMenuTree.routerLinkActive = 'active';
        leftMenuTree.routerLink = item.href;
        leftMenuTree.class = item.icon;
        leftMenuTree.name = item.name;
        const subMenu = new Array<LeftMenu>();
        leftMenuTree.subMenus = subMenu;
        return leftMenuTree;
    }

    flatMap(groupMap: Map<string, Array<LeftMenu>>, parentId: string) {
        groupMap.get(parentId).forEach(e => {
            if (groupMap.get(e.menuId) !== undefined) {
                e.subMenus = groupMap.get(e.menuId);
                e.subMenus.forEach(sub => {
                    if (groupMap.get(sub.menuId) !== undefined) {
                        sub.subMenus = groupMap.get(sub.menuId);
                    }
                });
            }
        });
        return groupMap;
    }

    /**
     * 递归寻找一个菜单栏目的父节点
     * @param menu 菜单栏
     * @param parentID 菜单id
     */
    findMenuParentPoint(menu: LeftMenu, parentID: string): LeftMenu {
        if (menu.menuId === parentID) {
            return menu;
        } else {
            let temp;
            if (menu.subMenus !== undefined && menu.subMenus.length > 0) {
                menu.subMenus.forEach(e => {
                    console.log(e.menuId);
                    temp = this.findMenuParentPoint(e, parentID);
                    if (temp !== undefined) { return temp; }
                });
                if (temp !== undefined) {
                    return temp;
                }
            }

        }
    }

    hasChild(item: any) {
        return item.subMenus === undefined || item.subMenus.length === 0;
    }

}
